<?php 
namespace Home\Controller;
use HDPHP\Library\Controller;

/*
 * 问答页面控制器->继承公共控制器
 */

class AskController extends CommonController{
	
	public function index(){
//		//获取分类的子集
		$cid =(int)$_GET['cid'];
		//获取get传参 分类信息
		$asid =(int)$_GET['asid'];
		//合并sql语句
		$sql ="SELECT * FROM hd_ask AS a JOIN hd_user AS u ON a.uid=u.uid WHERE a.asid={$asid}";
		//获得合并后的数据
		$askData =M()->query($sql);
        if(!empty($askData)){
//           分配变量到模板页面
        $askData=$askData[0];    
        }
//		z($askData);
		$this->assign('askData',$askData);		
		//取回答表和用户表
		$answersql="SELECT * FROM hd_answer AS a JOIN hd_user AS u ON a.uid=u.uid WHERE asid={$asid}";
		$answeData =M()->query($answersql);	
		
//		z($answeData);
		$this->assign('answeData',$answeData);
		
		//取当前已采纳的用户数据
		$acceptsql ="SELECT * FROM hd_answer AS a JOIN hd_user AS u ON a.uid=u.uid WHERE asid={$asid} AND a.accept=1";
		$acceptData =M()->query($acceptsql);
		
		
//		z($acceptData);
		$this->assign('acceptData',$acceptData);
		//取待解决的相关问题sql
		$noasksql= "SELECT * FROM hd_ask WHERE solve=0 and cid={$cid}";
		//取出待解决的问题条数
		$noasksqlData=M()->query($noasksql);
//		z($noasksqlData);
		$this->assign('noasksqlData',$noasksqlData);	
		
		//传递右侧用户和最高数据
		$this->IndexRight();
		//传递公共导航分类数据
		$this->TopMenu();
		//传递公共面包屑数据
		$this->TopNav();
		//传递公共导航提问总数		
		$this->AskCount();
		//载入问答页模板
		$this->display();
	}
	
	public function Up(){
		if(IS_POST && isset($_SESSION['uid']) && isset($_SESSION['uname'])){	
//			z($_POST);
			//获取发布内容
			$content = $_POST['content'];	
			//获取时间戳	
			$time =time();	
			//获取uid	 用户id
			$uid =isset($_SESSION['uid'])?$_SESSION['uid']:NULL;
			//获取asid分类表asid是表单隐藏域传递过来
			$asid=$_POST['asid'];
			//插入问题表数据 
			$insetsql ="INSERT INTO hd_answer SET content='$content',time='$time',uid=$uid,asid=$asid";
			$insetData=M()->exec($insetsql);
			//更新回答数据表记录回答数量+1
			$useinsetsql = "UPDATE hd_ask SET answer=answer+1 WHERE asid={$asid}";
			$UPData=M()->exec($useinsetsql);
			$usernameinsetsql = "UPDATE hd_user SET answer=answer+1 WHERE uid={$uid}";
			$UPData=M()->exec($usernameinsetsql);
			$this->success('回答成功','./index.php');
		}
	}
	public function accept(){
		//获取get传参 分类信息
		$asid =(int)$_GET['asid'];	
		$acceptsql = "UPDATE hd_answer SET accept=1 WHERE asid=$asid";
		$acceptData =M()->exec($acceptsql);
		$acceptuse ="UPDATE hd_user SET accept=accept+1,exp=exp+100 WHERE uid={$_SESSION['uid']}";
		$acceptuseData =M()->exec($acceptuse);
		$this->success('采纳成功','index.php');
	}
	
}